package com.google.code.drift.api;

import java.util.Collection;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

public class UserInfo {

	// 用户位置：纬度
	private int latitude;

	// 用户位置：经度
	private int longitude;

	// 用户昵称 默认:无名氏
	private String nick;

	// 用户唯一ID
	private int userId;

	// 1:男 0:女 2:保密 默认保密
	private int sex;

	// 用户所在聊天室的ID，实际上是圈主的ID
	private int roomId = -1;

	// 用户所在聊天室的在线人的列表
	// 这个是将废弃的属性
	private HashMap<Integer, UserInfo> roomPersonMap = new HashMap<Integer, UserInfo>();

	// 用户周围在线的人的列表
	private ConcurrentHashMap<Integer, UserInfo> nearbyPersonMap = new ConcurrentHashMap<Integer, UserInfo>();

	public UserInfo() {

	}

	/**
	 * 是否在漂浮着，就是既没创聊天室，又没加入聊天室
	 * 
	 * @return
	 */
	public boolean isDrifting() {
		return (roomId == -1);
	}

	public boolean isNotDrifting() {
		return !(isDrifting());
	}

	/**
	 * 是否为聊天室的主人
	 * 
	 * @return
	 */
	public boolean isRoomOwner() {
		return (userId == roomId);
	}

	/**
	 * 是否加入了聊天室
	 * 
	 * @return
	 */
	public boolean isInOtherRoom() {
		return (!this.isDrifting() && !this.isRoomOwner());
	}

	public void setLocation(int latitude, int longitude) {
		this.latitude = latitude;
		this.longitude = longitude;
	}

	public int getLatitude() {
		return latitude;
	}

	public int getLongitude() {
		return longitude;
	}

	public String getNick() {
		return nick;
	}

	public void setNick(String userNick) {
		this.nick = userNick;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int userSex) {
		this.sex = userSex;
	}

	public int getRoomId() {
		return roomId;
	}

	public void setRoomId(int inChatRoomId) {
		this.roomId = inChatRoomId;
	}

	public UserInfo getUserFromNearbyPerson(int id) {
		return this.nearbyPersonMap.get(id);
	}

	public ConcurrentHashMap<Integer, UserInfo> getNearbyPersons() {
		return nearbyPersonMap;
	}

	public Collection<UserInfo> getNearbyPersonListFromHashMap() {
		return nearbyPersonMap.values();
	}

	public void addNearbyPerson(UserInfo userInfo) {
		this.nearbyPersonMap.put(userInfo.getUserId(), userInfo);
	}

	public UserInfo removeNearbyPerson(int id) {
		return this.nearbyPersonMap.remove(id);
	}

	public void clearNearbyPersons() {
		this.nearbyPersonMap.clear();
	}

	public HashMap<Integer, UserInfo> getRoomPersons() {
		return roomPersonMap;
	}

	public synchronized void addRoomPerson(UserInfo userInfo) {
		this.roomPersonMap.put(userInfo.getUserId(), userInfo);
	}

	public UserInfo getUserFromRoomPerson(int id) {
		return roomPersonMap.get(id);
	}

	public void removeRoomPerson(UserInfo userInfo) {
		this.roomPersonMap.remove(userInfo.getUserId());
	}

	public void clearRoomPersons() {
		this.roomPersonMap.clear();
	}

	public Collection<UserInfo> getRoomPersonListFromHashMap() {
		return roomPersonMap.values();
	}

}